import {
  querySysDic,postSysDic,
  querySysDicDetail,
  postSysDicDetail
} from '../services/system';

export default {
  namespace: 'sysdic',

  state: {
    data: {
      list: [],
      pagination: {},
    },
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(querySysDic, payload);
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *fetchdetail({ payload }, { call, put }) {
      const response = yield call(querySysDicDetail, payload);
      yield put({
        type: 'save',
        payload:{
          ...response,
        },
      });
    },
    *add({ payload, callback }, { call, put }) {
      const response = yield call(postSysDic, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      if (callback) callback();
    },
    //新增及修改
    *adddetail({ payload, callback }, { call, put }) {
      const {type} = payload;
      const {success,id} = yield call(postSysDicDetail, payload);
      if(success){yield put({type: 'fetchdetail',payload:{id}});}
        // yield put({
        //   type: 'fetchdetail',
        //   payload:{
        //     id:id
        //   }
        // });
      // }
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
    clear() {
      return {
        data: {
          list: [],
          pagination: {},
        }
      };
    },
  },
};
